package cn.ac.jaas.isc.vegetable.admin.config;

import cn.ac.jaas.isc.vegetable.admin.service.SysOauthClientDetailsService;
import cn.ac.jaas.isc.vegetable.admin.service.SysTenantService;
import cn.ac.jaas.isc.vegetable.common.core.constant.CacheConstants;
import cn.ac.jaas.isc.vegetable.common.data.tenant.TenantBroker;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author 张高昌
 * @date 2022/4/23 15:17
 * @description: 功能描述
 */
@Slf4j
@Service
@RequiredArgsConstructor
@SuppressWarnings("all")
public class ClientDetailsInitRunner {

    private final SysOauthClientDetailsService clientDetailsService;

    private final SysTenantService tenantService;

    private final RedisTemplate redisTemplate;

    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class, ClientDetailsInitEvent.class})
    public void initClientDetails() {
        log.debug("初始化客户端信息开始 ");

        // 1. 查询全部租户循环遍历
        tenantService.list().forEach(tenant -> {
            TenantBroker.runAs(tenant.getId(), tenantId -> {
                // 2. 查询当前租户的所有客户端信息 (排除客户端扩展信息为空)
                clientDetailsService.list().stream().filter(client -> {
                    return StrUtil.isNotBlank(client.getAdditionalInformation());
                }).forEach(client -> {
                    // 3. 拼接key 1:client_config_flag:clinetId
                    String key = String.format("%s:%s:%s", tenantId, CacheConstants.CLIENT_FLAG, client.getClientId());
                    // 4. hashkey clientId 保存客户端信息
                    redisTemplate.opsForValue().set(key, client.getAdditionalInformation());
                });
            });
        });

        log.debug("初始化客户端信息结束 ");
    }

    /**
     * 客户端刷新事件
     */
    public static class ClientDetailsInitEvent extends ApplicationEvent {

        public ClientDetailsInitEvent(Object source) {
            super(source);
        }

    }
}
